<?php
class Questions extends Admin_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
		$data['categories'] = new Question_category;
		$data['categories']->order_by('id','desc')->get_page();
		$this->template->append_metadata(js_checkbox());
		$this->template->build('admin/question_category_index',$data);
	}
	
	function answer_index($id)
	{
		$data['emails'] = new Answer_email;
		$data['emails']->where('question_category_id',$id)->order_by('id','desc')->get_page();
		$this->template->build('admin/question_answer_index',$data);
	}
	
	function answer_view($id)
	{
		$data['email'] = new Answer_email($id);
		$this->template->build('admin/question_answer_view',$data);
	}
	
	function form($id = NULL)
	{	
		$data['category'] = new Question_category($id);
		$this->template->append_metadata(js_lightbox());
		$this->template->build('admin/question_category_form',$data);	
	}
	
	function save($id=FALSE)
	{
		if($_POST)
		{
			$category = new Question_category($id);
			$category->user_id = $this->session->userdata('id');
			$category->from_array($_POST);
			$category->save();
			
		
			foreach($_POST['title'] as $key => $title)
			{
					$question = new Question(@$_POST['question_id'][$key]);
					if($title)
					{
						$question->title = $title;
						$question->question_category_id = $category->id;
						$question->save();
					}		
			}
			
			set_notify('success', lang('save_data_complete'));
		}
		redirect('questions/admin/questions/form/'.$category->id);
	}
	
	function delete($id)
	{
		if($id)
		{
			$category = new Question_category($id);
			$category->question->delete_all();
			$category->delete();
			set_notify('success', lang('delete_data_complete'));
		}
		redirect('questions/admin/questions/');
	}
	
	function delete_question($id)
	{
		if($id)
		{
			$question = new Question($id);
			$question->answer->delete_all();
			$question->delete();
		}
	}
	
	function approve($id)
	{
		if($_POST)
		{
			$category = new Question_category($id);
			$category->from_array($_POST);
			$category->save();
			$category->clear();
			$category->where('id <>', $id)->get();
			$category->update_all('active',0);
		}
	}
	
}
?>